package com.hisense.hiioc.hismartplate.base;

import com.hisense.hiioc.hismartplate.entity.response.BaseRes;
import com.hisense.hiioc.hismartplate.exception.HttpNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @Author: almo
 * @Date: 2018/12/5 下午12:48
 * @Version 1.0
 */
public class BaseController<S extends BaseService> {

    @Autowired
    protected S defaultService;

    @ExceptionHandler(HttpNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    protected BaseRes httpNoFound() {
        return generateHttpBadRes(HttpStatus.NOT_FOUND.value(), "no data available");
    }

    protected <T> BaseRes<T> generateHttpOkRes(T t) {
        BaseRes<T> res = new BaseRes<>();
        res.setErr_no(HttpStatus.OK.value());
        res.setErr_msg("success");
        res.setSST(t);
        return res;
    }

    protected BaseRes generateHttpBadRes(int errNo, String errMsg) {
        BaseRes res = new BaseRes();
        res.setErr_no(errNo);
        res.setErr_msg(errMsg);
        return res;
    }

}
